package 力扣日常刷题.木22一月.第05天0112;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 帅小伙
 * @date 2022/1/12
 * @description
 */
public class Demo06回溯含有重复元素集合的全排列 {


    ArrayList<List<Integer>> ans = new ArrayList<>();
    boolean isUsed[] ;

    public List<List<Integer>> permuteUnique(int[] nums) {
        isUsed = new boolean[nums.length];
        Arrays.sort(nums);
        dfs(nums,0,new ArrayList<Integer>());
        return ans;
    }

    void dfs(int[] nums,int index,ArrayList<Integer> list) {
        if(index == nums.length){
            if(list.size() == nums.length){
                ans.add(new ArrayList<Integer>(list));
            }
            return ;
        }
        int last =-11;
        for (int i = 0; i < nums.length; i++) {
            if(!isUsed[i] && last != nums[i]){
                list.add(nums[i]);
                isUsed[i] = true;
                dfs(nums,index+1,list);
                isUsed[i] = false;
                list.remove(list.size() - 1);
                last = nums[i];
            }
        }
    }


}
